package com.models;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by TimeLiar on 2015/9/17.
 * All Hail The King !!!!!!
 */
public class User {
    private String username;
    private String password;
    private Map<String,String> permission;
    public User(){
        permission = new HashMap<>();
    }
    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public Map<String, String> getPermission() {
        return permission;
    }

    public void setPermission(Map<String, String> permission) {
        this.permission = permission;
    }

    @Override
    public String toString() {
        StringBuilder builder = new StringBuilder();
        builder.append("{");
        builder.append("\"username\":").append("\"").append(username).append("\",");
        builder.append("\"password\":").append("\"").append(password).append("\",");
        if(permission != null){
            builder.append("\"permission\":").append("{");
            for(String key:permission.keySet()){
                builder.append("\"").append(key).append("\":\"").append(permission.get(key));
                builder.append("\",");
            }
            if(builder.charAt(builder.length() - 1) == ','){
                builder.delete(builder.length() - 1, builder.length());
            }
            builder.append("}");
        }
        if(builder.charAt(builder.length() - 1) == ','){
            builder.delete(builder.length() - 1, builder.length());
        }
        builder.append("}");
        return  builder.toString();
    }

    @Override
    public boolean equals(Object object) {
        return !(object == null || !(object instanceof User))
                && username != null
                && username.equals(((User) object).getUsername());
    }
}
